<?php
/**
 * Created by PhpStorm.
 * User: tengwei
 * Date: 2018/9/7
 * Time: 上午11:08
 */

class PayCool extends M_Controller{

    public function library_stage(){
        //获取订单金额
        $oid = $this->input->get_post("oid");
        $order = $this->db->where("id",$oid)->get("course_order")->row_array();
        if(empty($order)){
            $this->msg("该订单不存在或者已经删除");
        }
        //获取已经绑定的信用卡信息
        $cards = $this->db->where("uid", get_cookie('user_id'))->get("user_band_card")->result_array();
        //获取银行
        $banks = getBank();

        $this->template->assign(array(
            "order"=>$order,
            "cards"=>$cards,
            "banks"=>$banks
        ));

        $this->template->display("../gw/library_stage.html");
    }

    public function credit_card_binding(){
        $bank = $this->input->get_post("bank");
        $nums = $this->input->get_post("nums");
        $oid = $this->input->get_post("oid");
        $order = $this->db->where("id",$oid)->get("course_order")->row_array();
//        if(IS_POST){
//
//
//        }
        $this->template->assign(array(
            "bank"=>$bank,
            "nums"=>$nums,
            "oid"=>$oid,
            "order"=>$order,
        ));
        $this->template->display("../gw/credit_card_binding.html");
    }

    //绑卡操作
    public function bandCard(){
        if(IS_POST){
            $nums = $this->input->get_post('nums');
            $applyId = $this->input->get_post("applyId");//申请id
            $payId = $this->input->get_post("payId");
            $oid = $this->input->get_post("oid");
            $cardNo = $this->input->get_post("cardNo");
            $month = $this->input->get_post('month');
            $year = $this->input->get_post("year");
            $safety_code = $this->input->get_post("safety_code");
            $name = $this->input->get_post("name");
            $idcard = $this->input->get_post('idcard');
            $phone = $this->input->get_post("phone");
            $code = $this->input->get_post("code");
            $bank = $this->input->get_post('bank');
            $order = $this->db->where("id",$oid)->get("course_order")->row_array();
            if(empty($order)){
                $this->msg("该订单不存在或者已经删除");
            }
            if(empty($payId)){
                $this->msg("支付流水不能为空");
            }

            $data = array(
                "orderId"=>$order['order_no'],
                "payId"=>$payId,
                "applyId"=>$applyId,
                "orderAmount"=>$order['totalprice'],
                "payAmount"=>$order['totalprice'],
                "smsVerifyCode"=>$code,
                "callbackURL"=>dr_user_url("pay/notify")
            );
            $risk = array(
                "deviceType"=>equipType('pc'),
                'ip'=> $_SERVER['REMOTE_ADDR'],
            );
            $param = array(
                "uid"=>get_cookie("user_id"),
                "midTransaction"=>midPlatform,
                "opType"=>6002,
                "time"=>microtime(true)*1000,
                "data"=>$data,
                "risk"=>$risk
            );
            $json = json_encode($param);
            $this->load->library("secooCrypt");
            $encryptObj = new SecooCrypt();
            $result = $encryptObj->requestEncrypt(AEC_KEY,MD5_KEY,midPlatform,VERSION,$json);//加密结果
            $res = cur_post(KU_FENG_QI_URL,$result);
            $obj = json_decode($res,true);
            $resultJson = "{\"sign\":\"".$obj['sign']."\",\"data\":\"".$obj['data']."\"}";
            $m = $encryptObj->responseDecrypt(AEC_KEY,MD5_KEY,$resultJson);//解密结果
            $resObj = json_decode($m,true);
//            print_r($resObj);
//            exit;
            if($resObj['result'] == 200){
                //开始存库
                $Arr = array(
                    'uid'=>get_cookie('user_id'),
                    'cardNo'=>$cardNo,
                    'safetyNo'=>$safety_code,
                    'username'=>$name,
                    'idCardNo'=>$idcard,
                    'phone'=>$phone,
                    'addtime'=>time(),
                    'bank'=>$bank
                );
                $result = $this->db->insert("user_band_card",$Arr);
                if($result){
                    $this->db->where('order_no',$order['order_no'])->update("course_order",array("applyid"=>$applyId));
                    $this->msg("绑卡并支付成功",dr_user_url('pay/myorder'));
                }
            }else{
                $this->msg($resObj.$result);
            }

        }
    }

    //获取信用卡短信验证码
    public function smsCard(){
        $cardNo = $this->input->get_post("cardNo");
        $year = $this->input->get_post("year");
        $month = $this->input->get_post("month");
        $safety_code = $this->input->get_post("safety_code");
        $username = $this->input->get_post("username");
        $idcard = $this->input->get_post("idcard");
        $phone = $this->input->get_post("phone");
        $bank = $this->input->get_post("bank");
        $nums = $this->input->get_post("nums");
        $course_name = $this->input->get_post("course_name");
        $oid = $this->input->get_post("oid");
        $order = $this->db->where("id",$oid)->get("course_order")->row_array();

        $data = array(
            "userName"=>$username,
            "idCardNo"=>$idcard,
            "bankTel"=>$phone,
            "bankCode"=>$bank,
            "bankNo"=>$cardNo,
            "bankExpireDate"=>"20".$year."/".$month,
            "bankCvn2"=>$safety_code,
            "orderId"=>$order['order_no'],
            "orderAmount"=>$order['totalprice'],
            "payAmount"=>$order['totalprice'],
            "stageNum"=>$nums,
            "productInfo"=>$course_name
        );
        $risk = array(
            "deviceType"=>equipType('pc'),
            'ip'=>$_SERVER['HTTP_X_FORWARDED_FOR'],
        );
        $param = array(
            "uid"=>get_cookie("user_id"),
            "midTransaction"=>midPlatform,
            "opType"=>6001,
            "time"=>microtime(true)*1000,
            "data"=>$data,
            "risk"=>$risk
        );
        $json = json_encode($param);
        $this->load->library("secooCrypt");
        $encryptObj = new SecooCrypt();
        $result = $encryptObj->requestEncrypt(AEC_KEY,MD5_KEY,midPlatform,VERSION,$json);//加密结果
        $res = cur_post(KU_FENG_QI_URL,$result);
        $obj = json_decode($res,true);
//        print_r($obj);
//        exit;
        $resultJson = "{\"sign\":\"".$obj['sign']."\",\"data\":\"".$obj['data']."\"}";
        $m = $encryptObj->responseDecrypt(AEC_KEY,MD5_KEY,$resultJson);//解密结果
        //{"data":{"applyId":"29002","bindStatus":10},"result":200}
        //我们这边做数据存处理

        echo $m;exit;
    }



    //绑卡后直接消费交易
    public function directtrade(){
        if (IS_POST){
            $code = $this->input->get_post("contant_code");
            $payId = $this->input->get_post("payId");
            $oid = $this->input->get_post("oid");
            $order = $this->db->where("id",$oid)->get("course_order")->row_array();
            $data = array(
                "payId" => $payId,
                "orderId" =>$order['order_no'],
                "orderAmount"=>$order['totalprice'],
                "payAmount"=>$order['totalprice'],
                "smsVerifyCode"=>$code,
                "callbackURL"=>dr_user_url("paycool/notify")
            );
            $risk = array(
                "deviceType"=>equipType('pc'),
                'ip'=> $_SERVER['REMOTE_ADDR'],
            );
            $param = array(
                "uid"=>get_cookie("user_id"),
                "midTransaction"=>midPlatform,
                "opType"=>2002,
                "time"=>microtime(true)*1000,
                "data"=>$data,
                "risk"=>$risk
            );

            if(empty($order)){
                $this->msg("该订单不存在或者已经删除");
            }
            if(empty($payId)){
                $this->msg("支付流水不能为空");
            }
            $json = json_encode($param);
            $this->load->library("secooCrypt");
            $encryptObj = new SecooCrypt();
            $result = $encryptObj->requestEncrypt(AEC_KEY,MD5_KEY,midPlatform,VERSION,$json);//加密结果
            $res = cur_post(KU_FENG_QI_URL,$result);
            $obj = json_decode($res,true);
            $resultJson = "{\"sign\":\"".$obj['sign']."\",\"data\":\"".$obj['data']."\"}";
            $m = $encryptObj->responseDecrypt(AEC_KEY,MD5_KEY,$resultJson);//解密结果
            $resObj = json_decode($m,true);
//            print_r($resObj);
//            exit;
            if($resObj['result'] == 200){
                if($result){
                    $this->msg("库分期支付成功",dr_user_url('pay/myorder'));
                }
            }else{
                $this->msg($resObj.$result);
            }
        }
    }

    //绑卡后直接消费交易验证码
    public function smstrade(){
        $phone = $this->input->get_post("phone");
        $cardinfo = $this->db->where("phone",$phone)->get("user_band_card")->row_array();
        $nums = $this->input->get_post("nums");
        $course_name = $this->input->get_post("course_name");
        $oid = $this->input->get_post("oid");
        $order = $this->db->where("id",$oid)->get("course_order")->row_array();
        $data = array(
            "bankNo"=>$cardinfo['cardNo'],
            "orderId"=>$order['order_no'],
            "orderAmount"=>$order['totalprice'],
            "payAmount"=>$order['totalprice'],
            "stageNum"=>$nums,
            "productInfo"=>$course_name
        );

        $risk = array(
            "deviceType"=>equipType('pc'),
            'ip'=>$_SERVER['HTTP_X_FORWARDED_FOR'],
        );
        $param = array(
            "uid"=>get_cookie("user_id"),
            "midTransaction"=>midPlatform,
            "opType"=>2001,
            "time"=>microtime(true)*1000,
            "data"=>$data,
            "risk"=>$risk
        );
        $json = json_encode($param);
        $this->load->library("secooCrypt");
        $encryptObj = new SecooCrypt();
        $result = $encryptObj->requestEncrypt(AEC_KEY,MD5_KEY,midPlatform,VERSION,$json);//加密结果
        $res = cur_post(KU_FENG_QI_URL,$result);
        $obj = json_decode($res,true);
        $resultJson = "{\"sign\":\"".$obj['sign']."\",\"data\":\"".$obj['data']."\"}";
        $m = $encryptObj->responseDecrypt(AEC_KEY,MD5_KEY,$resultJson);//解密结果
        echo $m;exit;

    }


    //库分期回调函数
    public function notify(){
        $this->load->library("secooCrypt");
        $encryptObj = new SecooCrypt();
        $json = file_get_contents('php://input', 'r');
        if(!empty($json)){
            $m = $encryptObj->responseDecrypt(AEC_KEY,MD5_KEY,$json);//解密结果
            $resObj = json_decode($m,true);
            if($resObj['data']['status'] == 200){
                ///改变订单状态
                if(!empty($resObj['data']['orderId'])){
                    $out_trade_no = $resObj['data']['orderId'];
                    @file_get_contents(WEBSITE_PAY_URL."/index.php?s=user&c=order&m=modifyOrderStatus".'&sign=SD45645sdfmlsdfjusd12132snxnzchaqllqaqwqweqewq45dvs&order_no='.$out_trade_no.'&pay_type=kfq');
                }
                echo "SUCCESS";
            }

        }else{
            $this->msg("返回有误");
        }
    }

}